//
//  CCSDLoginTextFild.m
//  Ccsd_user
//
//  Created by 张露杰 on 17/9/7.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "HBHDLoginTextFild.h"

@interface HBHDLoginTextFild()<UITextFieldDelegate>
{
    NSInteger _maxNum;
}
@end

@implementation HBHDLoginTextFild

- (instancetype)initWithFrame:(CGRect)frame imageName:(NSString *)imageName placeholder:(NSString *)placeholder keyboardType:(UIKeyboardType)keyboardType maxNum:(NSInteger)maxNum
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(frame.origin.x, frame.origin.y, SCREENW - 60*screenW_6, 55*screenW_6);
        
        [self creatViewWithImageName:imageName placeholder:placeholder keyboardType:keyboardType];
        if (maxNum > 0) {
            _maxNum = maxNum;
        }
    }
    return self;
}

-(void)creatViewWithImageName:(NSString *)imageName placeholder:(NSString *)placeholder keyboardType:(UIKeyboardType)keyboardType
{
    UIImageView *imageV = [TomamaUI createImageView:CGRectMake(12.5*screenW_6, 0, 14*screenW_6, 17*screenW_6) backGroundImageV:imageName];
    [self addSubview:imageV];
    imageV.center = CGPointMake(imageV.center.x, self.frame.size.height/2);
    
//    CGFloat length = 0;
//    if (type == CCSDLoginTextFildPassWordType) {
//        UIButton *btn = [TomamaUI createButton:0 Frame:CGRectMake(self.frame.size.width - 22*screenW_6, 0, 17*screenW_6, 11*screenW_6) targ:self sel:@selector(hiddenText:) titleColor:nil font:nil title:nil image:@"Login_eyeoff" backGroundImage:nil];
//        [btn setImage:[UIImage imageNamed:@"Login_eyeon"] forState:UIControlStateSelected];
//        [self addSubview:btn];
//        btn.center =CGPointMake(btn.center.x, imageV.center.y);
//        length = 22*screenW_6;
//    }else if (type == CCSDLoginTextFildMsgType){
//        UIButton *btn = [TomamaUI createButton:0 Frame:CGRectMake(self.frame.size.width - 65*screenW_6, 0, 60*screenW_6, 15*screenW_6) targ:self sel:@selector(sendMsg:) titleColor:[UIColor colorWithHexString:@"33a7e0"] font:[UIFont systemFontOfSize:15*screenW_6] title:@"发送" image:nil backGroundImage:nil];
//        [self addSubview:btn];
//        btn.center =CGPointMake(btn.center.x, imageV.center.y);
//        length = 22*screenW_6;
//    }
    
    UITextField *textF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageV.frame) + 20*screenW_6, 0, self.frame.size.width - CGRectGetMaxX(imageV.frame) - 35*screenW_6, 15*screenW_6)];
    textF.center =CGPointMake(textF.center.x, imageV.center.y);
    textF.font = [UIFont systemFontOfSize:15*screenW_6];
    textF.textColor = TextGray;
    textF.placeholder = placeholder;
    [textF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    if (keyboardType) {
        textF.keyboardType = keyboardType;
    }
    textF.tag = 100;
    textF.delegate = self;
    [self addSubview:textF];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#9dd8d8"];
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:textF.placeholder attributes:dict];
    [textF setAttributedPlaceholder:attribute];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 1, self.frame.size.width, 1)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#B7E5E5"];
    [self addSubview:lineView];
}

-(void)textFieldDidChange:(UITextField *)textField
{
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position && _maxNum) {
        if (textField.text.length > _maxNum) {
            textField.text = [textField.text substringToIndex:_maxNum];
        }
    }
    if (_CCSDLoginTextFildblock) {
        _CCSDLoginTextFildblock(textField.text);
    }
}

@end
